<template>
  <div class="app-container">
    <el-card>
      泵房系统：<el-select v-model="pump">
        <el-option v-for="(item,index) in pumps" :key="index" :label="item.name" :value="item.serialno" />
      </el-select>
    </el-card>
    <div class="h12" />
    <el-card class="box-card">
      <div slot="header" class="clearfix flex">
        <b>灌水量统计</b>
      </div>
      <el-row>
        <el-col :span="5">
          <dl class="card flex">
            <dt><img src="" alt=""></dt>
            <dd><b>0.00 m³</b><i>今日灌水量</i></dd>
          </dl>
        </el-col>
        <el-col :span="5">
          <dl class="card flex">
            <dt><img src="" alt=""></dt>
            <dd><b>0.00 m³</b><i>本月灌水量</i></dd>
          </dl>
        </el-col>
        <el-col :span="5">
          <dl class="card flex">
            <dt><img src="" alt=""></dt>
            <dd><b>0.00 m³</b><i>本年灌水量</i></dd>
          </dl>
        </el-col>
      </el-row>
    </el-card>
    <div class="h12" />
    <el-card class="box-card">
      <div slot="header" class="clearfix flex" style="align-items:center">
        <b>系统每小时累积流量</b>
        <el-date-picker
          v-model="date"
          class="mlauto"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
          align="center"
        />
      </div>
      <ve-histogram :data="chartDataDay" :height="chartHeight" :extend="chartExtend" />
    </el-card>
    <div class="h12" />
    <el-card class="box-card">
      <div slot="header" class="clearfix flex" style="align-items:center">
        <b>系统每日灌溉量</b>
        <el-date-picker
          v-model="daterange"
          class="mlauto"
          type="daterange"
          range-separator="~"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          align="center"
        />
      </div>
      <ve-histogram :data="chartDataMonth" :height="chartHeight" :extend="chartExtend" />
    </el-card>
    <div class="h12" />
  </div>
</template>

<script>
import { getCurrentDate, getNextDate, getDaterange } from '@/utils'
export default {
  data() {
    return {
      date: getCurrentDate(),
      daterange: [getNextDate(getCurrentDate(), -30), getCurrentDate()],
      pump: '1',
      pumps: [
        { name: '系统1', serialno: '1' },
        { name: '系统2', serialno: '2' }
      ],
      chartDataDay: {
        columns: ['时间', '灌溉量'],
        rows: []
      },
      chartDataMonth: {
        columns: ['日期', '灌溉量'],
        rows: []
      },
      chartHeight: '240px',
      chartExtend: {
        series: {
          showSymbol: false,
          stack: 'intelirri',
          barMaxWidth: 20
        },
        color: ['#fc8452', '#409eff', '#67c23a'],
        grid: {
          left: '2%',
          right: '2%',
          bottom: '0%',
          top: '18%',
          containLabel: true
        }
      }
    }
  },
  watch: {
    date: {
      handler(newVal) {
        this.chartDataDay = this.getDayIrrigation(newVal)
      },
      immediate: true
    },
    daterange: {
      handler(newVal) {
        this.chartDataMonth = this.getDaterangeIrrigation(newVal)
      },
      immediate: true
    }
  },
  methods: {
    getDayIrrigation(date) {
      const res = {
        columns: ['时间', '灌溉量'],
        rows: []
      }
      const dd = new Date()
      const hourEnd = date === getCurrentDate() ? dd.getHours() : 24

      for (let i = 1; i <= hourEnd; i++) {
        res.rows.push({ '时间': i.toString().padStart(2, 0) + ':00', '灌溉量': 0 })
      }

      return res
    },
    getDaterangeIrrigation(daterange) {
      const res = {
        columns: ['日期', '灌溉量'],
        rows: []
      }
      const arr = getDaterange(daterange)
      arr.forEach((item) => {
        res.rows.push({ '日期': item, '灌溉量': 0 })
      })

      return res
    }
  }
}
</script>

<style lang="scss" scoped>
.card{
    height: 87px;
    background: #f0f2f9;
    border-radius: 4px;
    text-align: center;
    margin-right: 15px;
    padding: 20px;
    box-sizing: border-box;
    dd{
      display:flex;
      flex-direction: column;
      margin-left:12px;
      text-align: left;
      padding:4px 0;
      i{
        font-style: normal;
        font-size:14px;
        color:#999;
        margin-top:6px;
      }
    }
}
</style>
